package com.kaishengit.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Employee;
import com.kaishengit.service.GoalService;
import com.kaishengit.service.TaskEmployeeService;
import com.kaishengit.util.StringUtil;
import com.kaishengit.util.db.Page;

public class Goal extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p = request.getParameter("p");
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		String employeeid = employee.getId();
		String projectid = (String) session.getAttribute("pid");

		int pageNum = 1;
		if(StringUtil.isNotEmpty(p)) {
			try {
				pageNum = Integer.parseInt(p);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		
		Page<com.kaishengit.entity.Goal> page =( new GoalService()).findByPageAndEmployeeIdAndProjectId(pageNum,employeeid,projectid); 
		for(int j=0;j<page.getList().size();j++){
			com.kaishengit.entity.Goal goal = page.getList().get(j);
			List<com.kaishengit.entity.TaskEmployee> list = new TaskEmployeeService().findByGoalId(goal.getId());
			double goalrate = 0;
			for(int i=0;i<list.size();i++){
				goalrate += (list.get(i)).getTaskrate();
				
			}
			goal.setRate(goalrate);
		}
		
		
		request.setAttribute("page", page);
		request.getRequestDispatcher("/WEB-INF/views/goal.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
